package com.onesports.intelligent.k12.polarlight.service.index;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.domain.dto.index.HomeDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.report.OrganizationHomePage;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.OrganizationHomePageVO;
import com.onesports.intelligent.k12.polarlight.mapper.report.OrganizationHomePageMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author FPH
 * @since 2022年7月19日11:43:24
 */
@Service
public class OrganizationHomePageService extends ServiceImpl<OrganizationHomePageMapper, OrganizationHomePage> {
    /**
     * 获取机构端简报
     * @return
     */
    public OrganizationHomePageVO getList(HomeDTO dto){
        final Integer init=0;
        Date date = new Date(dto.getDate().getTime() + 30 * 60 * 1000);
        List<OrganizationHomePage> organizationHomePage = list(new LambdaQueryWrapper<OrganizationHomePage>()
//                .lt(OrganizationHomePage::getCreatedDate,date)
//                .ge(OrganizationHomePage::getCreatedDate,dto.getDate())
                .eq(OrganizationHomePage::getOrganizationId, SecurityUtils.getOrganizationId())
                .orderByDesc(OrganizationHomePage::getStatisticalTime)
                );


        //初始化
        //将查找到的该机构下的所有count相加
        BigDecimal pinClassAmount = new BigDecimal(init);
        OrganizationHomePageVO result = OrganizationHomePageVO.builder().addApplyCount(init)
                .takeClassCount(init).arrangeClassCount(init).signUpCount(init)
                .absentCount(init).pinClassAmount(pinClassAmount).statisticalTime(dto.getDate())
                .leaveCount(init).currentClassCount(init).build();
        if (ParamUtils.isEmpty(organizationHomePage)){
            return result;
        }

        result.setAddApplyCount(organizationHomePage.stream().findFirst().get().getAddApplyCount());
        result.setTakeClassCount(organizationHomePage.stream().findFirst().get().getTakeClassCount());
        organizationHomePage.forEach(i->{
            result.setArrangeClassCount(result.getArrangeClassCount()+i.getArrangeClassCount());
            result.setSignUpCount(result.getSignUpCount()+i.getSignUpCount());
            result.setAbsentCount(result.getAbsentCount()+i.getAbsentCount());
            result.setPinClassAmount(result.getPinClassAmount().add(i.getPinClassAmount()));
            result.setCurrentClassCount(result.getCurrentClassCount()+i.getCurrentClassCount());
            result.setLeaveCount(result.getLeaveCount()+i.getLeaveCount());
        });
        return result;
    }
}
